home
diamond Go Premium
Data Engineering Path  ·  PySpark

DataFrame Filter & Where Operations

Filtering row records in PySpark using column expressions and SQL conditional strings.


What is the Filter / Where Operation?

The filter() and where() operations restrict row results in a DataFrame based on a boolean conditional clause, equivalent to the WHERE clause in relational databases.

In PySpark, filter() and where() are completely identical aliases—there is absolutely no functional or performance difference between them.


Syntax and Comparison Operators

You can build query conditions using Column object expressions or standard SQL comparison strings:

from pyspark.sql.functions import col

# Method A: SQL Query String (Quick, but not type-safe)
df.filter("salary > 80000 AND department = 'Engineering'")

# Method B: Column Object Expressions (Highly recommended, type-safe, supports multiple brackets)
df.filter((col("salary") > 80000) & (col("department") == "Engineering"))

Supported Operators (Column Objects)

  • AND: & (requires parentheses around each condition)
  • OR: | (requires parentheses around each condition)
  • NOT: ~ or .isNotNull() / .isNull()
  • Equality: ==
  • In-List: .isin("A", "B", "C")
  • String Match: .like("%pattern%") or .contains("sub")

Example Usage Pipeline

Below is a complete, copy-paste-ready PySpark script demonstrating advanced filtering:

from pyspark.sql import SparkSession
from pyspark.sql.functions import col

# 1. Setup local Spark session
spark = SparkSession.builder \
    .appName("DataFrame Filter Demo") \
    .master("local[*]") \
    .getOrCreate()

# 2. Dummy dataset (Hospital inventory)
data = [
    ("item_01", "Syringe", 120, "ICU"),
    ("item_02", "Stethoscope", 15, "Outpatient"),
    ("item_03", "Ventilator", 4, "ICU"),
    ("item_04", "Oxygen Mask", 0, "Emergency"),
    ("item_05", "Surgical Glove", 800, "Emergency"),
]
columns = ["item_id", "item_name", "stock_count", "department"]
df = spark.createDataFrame(data, columns)

# 3. Apply Multi-Condition Column Filters:
# - Stock must be low (less than 50)
# - Department must be either 'ICU' or 'Emergency'
filtered_low_stock = df.filter(
    (col("stock_count") < 50) & 
    (col("department").isin("ICU", "Emergency"))
)

# 4. Apply string condition (identical where clause)
filtered_outpatient = df.where("department = 'Outpatient'")

# 5. Show results
print("=== ICU/Emergency Low Stock Alerts ===")
filtered_low_stock.show(truncate=False)

print("=== Outpatient Ward Inventory ===")
filtered_outpatient.show(truncate=False)

Rendered Output:

=== ICU/Emergency Low Stock Alerts ===
+-------+-----------+-----------+----------+
|item_id|item_name  |stock_count|department|
+-------+-----------+-----------+----------+
|item_03|Ventilator |4          |ICU       |
|item_04|Oxygen Mask|0          |Emergency |
+-------+-----------+-----------+----------+

=== Outpatient Ward Inventory ===
+-------+-----------+-----------+----------+
|item_id|item_name  |stock_count|department|
+-------+-----------+-----------+----------+
|item_02|Stethoscope|15          |Outpatient|
+-------+-----------+-----------+----------+
Find this content helpful? ☕ Buy me a coffee

Entity Details

Create New Item

celebration
Enjoying the free content?

Create a free account to track your progress and save your place.

Create Free Account
help

Submit Technical Query

Have a question or run into an issue? Describe it below, upload an optional screenshot, and our engineering team will answer it!

image Attach image (optional)

Submit Feedback

build Free Developer Utility Free Tool
gavel

Privacy & Legal Disclaimer

1. Client-Side Browser Processing

All utility tools on DeepEngineerHub (including Image to PDF, Text Formatters, JSON Converters, and Encryptors) execute 100% locally within your client browser using WebAssembly and JavaScript. No uploaded images, text, or documents are transmitted, collected, or stored on remote servers.

2. Limitation of Liability ("As-Is" Provision)

Tools and services are provided free of charge for convenience and educational purposes "as-is" without warranties of any kind. DeepEngineerHub shall not be held liable for any data loss, formatting inconsistencies, or indirect damages resulting from tool usage.

3. Open Source & Third-Party Software

Certain utilities utilize open-source client libraries (such as jsPDF, Mermaid.js, Pyodide) licensed under MIT, Apache, or BSD open licenses. All intellectual property remains with their respective copyright holders.